package org.komarichin.struts2;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.komarichin.beans.IFile;
import org.komarichin.beans.service.IFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * <p>
 * &nbsp;
 * </p>
 * User: Michael Komarichin
 * Date: Jan 2, 2010
 * Time: 2:21:21 AM
 */
@Repository
public class FileManager extends BaseAction {
    private static final long serialVersionUID = -6893976641685378467L;

    private String path;
    private Long fileId;
    private String folderName;

    private File newFile;
    private String newFileContentType;
    private String newFileFileName;
    private String newFolderName;

    @Autowired
    private IFileService<Long> fileService;


    public String list() {

        if (StringUtils.isEmpty(path)) {
            path = "/";
        }

//        fileService.getFilesAndFolders(path);

        return SUCCESS;
    }

    public Collection getList() {

        if (StringUtils.isEmpty(path)) {
            path = "/";
        }

        return fileService.getFilesAndFolders(path);
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String view() throws Exception {
        HttpServletResponse res = ServletActionContext.getResponse();
        IFile file = fileService.getFile(fileId);
        OutputStream os = res.getOutputStream();
        res.setContentType(file.getContentType());
        os.write(file.getFile());
        return null;
    }


    public String createFolder() {
        IFile<Long> file = fileService.createBlank();
        file.setName(getFolderName());
        file.setFolder(getPath());
        fileService.save(file);
        return SUCCESS;
    }

    public String upload() throws Exception {
        ServletFileUpload upload = new ServletFileUpload();
        upload.setSizeMax(50000);
        HttpServletResponse res = ServletActionContext.getResponse();
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();

        try {
            FileItemIterator iterator = upload.getItemIterator(ServletActionContext.getRequest());
            while (iterator.hasNext()) {
                FileItemStream item = iterator.next();
                InputStream in = item.openStream();

                if (item.isFormField()) {
                    out.println("Got a form field: " + item.getFieldName());
                } else {
                    String fieldName = item.getFieldName();
                    String fileName = item.getName();
                    String contentType = item.getContentType();

                    out.println("--------------");
                    out.println("fileName = " + fileName);
                    out.println("field name = " + fieldName);
                    out.println("contentType = " + contentType);

                    String fileContents = null;
                    try {
                        fileContents = IOUtils.toString(in);
                        out.println("lenght: " + fileContents.length());
                        out.println(fileContents);
                    } finally {
                        IOUtils.closeQuietly(in);
                    }

                }
            }
        } catch (FileUploadBase.SizeLimitExceededException e) {
            out.println("You exceeded the maximu size ("
                    + e.getPermittedSize() + ") of the file ("
                    + e.getActualSize() + ")");
        } catch (Exception ex) {
            throw new ServletException(ex);
        }

        return SUCCESS;
    }

    public File getNewFile() {
        return newFile;
    }

    public void setNewFile(File newFile) {
        this.newFile = newFile;
    }

    public String getNewFileContentType() {
        return newFileContentType;
    }

    public void setNewFileContentType(String newFileContentType) {
        this.newFileContentType = newFileContentType;
    }

    public String getNewFileFileName() {
        return newFileFileName;
    }

    public void setNewFileFileName(String newFileFileName) {
        this.newFileFileName = newFileFileName;
    }

    public String getNewFolderName() {
        return newFolderName;
    }

    public void setNewFolderName(String newFolderName) {
        this.newFolderName = newFolderName;
    }

    public Long getFileId() {
        return fileId;
    }

    public void setFileId(Long fileId) {
        this.fileId = fileId;
    }

    public String getFolderName() {
        return folderName;
    }

    public void setFolderName(String folderName) {
        this.folderName = folderName;
    }
}
